/**
 * Copyright (C) 2010 OpenWeb Technology Inc.
 */

package photosalvage.rs;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import photosalvage.PhotoSalvageConstants;

import com.google.inject.Provider;


/**
 * @author shumpei
 * 
 */
public class AuthCheckInterceptor implements MethodInterceptor {
	@Inject
	Provider<HttpServletRequest> requestRef;

	@Inject
	Provider<HttpServletResponse> responseRef;

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		if (invocation.getMethod().isAnnotationPresent(AuthNotRequired.class)) {
			return invocation.proceed();
		}
		HttpServletRequest request = requestRef.get();
		HttpSession session = request.getSession(false);
		if (session == null) {
			return WebUtils.redirect2LoginPage();
		}
		if (session.getAttribute(PhotoSalvageConstants.SESSION_KEY_USER) == null) {
			return WebUtils.redirect2LoginPage();
		} else {
			return invocation.proceed();
		}
	}
}
